package com.znsb.msfq.push;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.znsb.msfq.R;
import com.znsb.msfq.activity.CommodityDetailsActivity;
import com.znsb.msfq.activity.MyMessageActivity;
import com.znsb.msfq.utils.JsonResult;
import com.znsb.msfq.utils.SPUtils;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * 名称：
 * 创建时间：2016/10/17.
 * 作者：杨广
 * 手机号：13330359437
 * 说明：
 */
public class TalkReceiver extends BroadcastReceiver {
    private static final String TAG = "TalkReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + bundle);

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, ">>>>收到了自定义消息" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            Log.i(TAG, ">>>>收到了自定义消息可选设置" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            Log.i(TAG, ">>>>收到了自定义消息可选设置" + bundle.getString(JPushInterface.EXTRA_EXTRA));
            sendLocalNotification(context, intent);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, ">>>>>>接受到推送下来的通知");
            Log.i(TAG, ">>>>用户点击打开了通知" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            Log.i(TAG, ">>>>用户点击打开了通知可选设置" + bundle.getString(JPushInterface.EXTRA_EXTRA));

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.i(TAG, ">>>>用户点击打开了通知");
            Log.i(TAG, ">>>>用户点击打开了通知" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            Log.i(TAG, ">>>>用户点击打开了通知可选设置" + bundle.getString(JPushInterface.EXTRA_EXTRA));
            String text = bundle.getString(JPushInterface.EXTRA_EXTRA);
            int ids = -110;
            try {
                JSONObject jsonObject = new JSONObject(text);
                ids = Integer.parseInt(JsonResult.getString(jsonObject, "goodsId"));
            } catch (Exception e) {
            }
            if (ids != -110) {
                Intent i = new Intent(context, CommodityDetailsActivity.class);
                i.putExtra("ids", ids);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            }


        } else {
            Log.d(TAG, ">>>>>>>>>>Unhandled intent - " + intent.getAction());
        }
    }

    private void sendLocalNotification(Context context, Intent intent) {
        // 通知id需要唯一，要不然会覆盖前一条通知
        int notifyId = (int) System.currentTimeMillis();
        if (intent != null) {

            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setAction(Intent.ACTION_MAIN);

            PendingIntent intentFilter = PendingIntent.getActivity(
                    context, 100, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            // 发送通知
            Notification notification = new NotificationCompat.Builder(
                    context)
                    .setContentTitle(intent.getExtras().getString(JPushInterface.EXTRA_MESSAGE))
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND
                            | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
                    .setLargeIcon(
                            BitmapFactory.decodeResource(
                                    context.getResources(),
                                    R.mipmap.app_icon))
                    .setContentIntent(intentFilter)
                    .setSmallIcon(R.mipmap.app_icon).build();
            NotificationManager manager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(100, notification);
        }

    }
}
